<?php
/**
 * redis 读取数据，MySQL 插入数据
 * User: lisgroup
 * Date: 2017/11/10
 * Time: 14:57
 */

include_once __DIR__.'/../init.php';

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// 1. 模拟数据
for ($i = 1; $i < 5; $i++) {
    $arr = [
        'bankcard' => '621626100000000001'.$i,
        'mobile' => '1598084704'.$i,
        'realname' => '汪中'.$i,
        'idcard' => '35082119870613451'.$i
    ];

    $rs = $redis->rPush('myList', json_encode($arr, JSON_UNESCAPED_UNICODE));
    var_dump($rs);
}


/*foreach ($arr as $k => $v) {
    $redis->rPush('myList', $v);
}*/

// 定时任务，每次执行 1000 条数据，生成 insert 语句
$sql = "INSERT INTO `bank4` (`bankcard`, `realname`, `idcard`, `mobile`) VALUES ";

for ($m = 0; $m < 10; $m++) {
    // json 格式数据
    $value = $redis->lPop('myList');
    if (!$value) break;
    $result = json_decode($value, true);
    // 拼接字符串
    $sql .= "('$result[bankcard]', '$result[realname]', '$result[idcard]', '$result[mobile]' ),";
}
$sql = rtrim($sql, ',');
echo $sql;

if (strlen($sql) > 75) {
    // 插入数据库
    $mysqli = new \Apps\lib\MysqlQuery();

    $result = $mysqli->query($sql);

    var_dump($result);
}


